<template>
  <div>
    <el-breadcrumb separator="/" class="submenu">
      <el-breadcrumb-item>
          <a href="javascript:;">新物料打样</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <div class="main-table">
      <el-tabs v-model="addNpcTask" @tab-click="handleClick">
        <el-tab-pane label="打样任务主信息" name="first">
          <npcTask :npcTaskForm="npcTaskForm" ref="npcTask" @changePurOrg="changePurOrg" @changeFactory="changeFactory"></npcTask>
        </el-tab-pane>
        <el-tab-pane label="打样明细" name="second" ref="npcTaskItem">
          <npcTaskItem @changeMaterialData="changeMaterialData" :purOrgId="purOrgId" :factoryId="factoryId" :npcItemData="npcItemData"></npcTaskItem>
        </el-tab-pane>
        <el-tab-pane label="选择供应商" name="thirdly">
          <vendors-transfer ref="trans" 
            @transAdd="addTrans" width="100%" :checkName="['vendorCode','vendorName']" 
            :title="{left:'可选供应商',right:'已选供应商'}"
            :searchForm="searchForm" :selectData="selectData" 
            :url="lib.config.host.system + '/system/orgmgt/organization/queryForNpcDatalist'">
          </vendors-transfer>
        </el-tab-pane>
        <el-tab-pane label="关联物料" name="fourthly">
          <npcTaskMapping :dataVendorList="dataVendorList" :dataMaterialList="dataMaterialList" :dataMatVendor="dataMatVendor"></npcTaskMapping>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div style="float: right; margin-right: 10px;" class="el_submit" >
      <el-button type="primary" icon="el-icon-time" @click="saveOrUpdate()">暂存</el-button>
      <el-button type="success" icon="el-icon-success" @click="saveAndPublish()">提交</el-button>
    </div>
  </div>
</template>
<script>
  import qs from 'qs';
  import NpcTask from './npcTask.vue'
  import NpcTaskItem from './npcTaskItem.vue'
  import vendorsTransfer from '@/components/common/select-ext/vendorsTransfer.vue';
  import NpcTaskMapping from './npcTaskMapping.vue'
  export default {
    components: {
      NpcTask,
      NpcTaskItem,
      vendorsTransfer,
      NpcTaskMapping
    },
    data() {
      return {
        addNpcTask: 'first',
        npcTaskForm:{
          attachmentUrl: '',
          attachmentName: '',
          notice:'',
          applyItemIds:[],
          shortMessage:'0',
          categoryCode:'',
        },
        dataVendorList:[],
        selectData:[],
        checkedData:[],
        dataMaterialList:[],
        npcItemData:[],
        dataMatVendor:[],
        // npcTask子件传递的值
        purOrgId:'',
        factoryId:'',
        searchForm:[
          {key:'vendorType',type:'',value:''},
				  {key:'vendorCodeOrVendorName',type:'text',placeholder:'请输入名称',value:''}
        ]
      };
    },
    created(){
      if(this.$route.query.checkedData && this.$route.query.dlvFlowId){ // 开发申请待办跳转
        this.checkedData =  this.$route.query.checkedData;
        // 赋值给主信息表单
        this.npcTaskForm = this.checkedData[0];
        this.npcTaskForm.npcType = this.$route.query.dlvFlowId;
        this.dataVendorList =this.checkedData;
        // 赋值给供应商穿梭框
        this.searchTrans();
      } else if(this.$route.query.checkedData){ // 采购申请跳转
        this.checkedData =  this.$route.query.checkedData;
        // 赋值给主信息表单
        this.npcTaskForm = this.checkedData[0];
        this.checkedData.forEach(element=>{
          let npcItemform = {};
          npcItemform.materialNo = element.materialCode;
          npcItemform.materialName = element.materialName;
          npcItemform.sampleNumber = element.purchaseQty;
          npcItemform.unit = element.purUnitCode;
          this.npcItemData.push(npcItemform);
        })
        this.changeMaterialData(this.npcItemData);
      }
    },
    methods: {
      handleClick(tab, event) {
        switch (this.addNpcTask){
          case 'first':
            break;
          case 'second':
            break;
          case 'thirdly':
            break;
          case 'fourthly':
            break;
          default:
            break;
        }
      },
      searchTrans(){
        this.checkedData.forEach(element=>{
          element.id = element.vendorId;
        })
        this.selectData = this.checkedData;
        this.$nextTick(()=>{
          this.$refs.trans.searchTrans({pageNo:1, pageSize:10});
        })
      },
      addTrans(data){
        this.dataVendorList = data
      },
      changeVendorData(data){
        this.dataVendorList = data;
      },
      changeMaterialData(data){
        this.dataMaterialList = data;
      },
      // 子组件传值触发
      changePurOrg(purOrgId){
        this.purOrgId = purOrgId;
      },
      // 子组件传值触发
      changeFactory(factoryId){
        this.factoryId = factoryId;
      },
      // 保存|更新
      saveOrUpdate(){
        // 获取子件的富文本内容
        let content = this.$refs.npcTask.getUEContentTxt();
        this.npcTaskForm.notice = content;
        this.npcTaskForm.id = null;
        this.checkedData.forEach(element1 =>{
          this.dataMatVendor.forEach(element2 =>{
            if(element1.vendorCode === element2.vendorCode){
              element2.applyItemId = element1.applyItemId;
              element2.applyNo = element1.applyNo;
            }
          })
        })
        let data={
          ...this.npcTaskForm,
          npcTaskItemList:this.dataMaterialList,
          npcTaskVendorList:this.dataVendorList,
          npcTaskMappingList:this.dataMatVendor,
        }
        debugger;
        console.log(data)
        this.axios.post(this.lib.config.host.vendorDlv + "/vendormgt/npc/npcTask/saveOrUpdate",data).then((res) => {
          if(res.data.success){
            // 发布后跳转到主页面
            this.$router.push('/Index/VendorDlv/NpcTask');
          }
        })
        .catch(function (error) {
            console.log(error);
        });
      },
      // 保存&发布
      saveAndPublish(){
         // 获取子件的富文本内容
        let content = this.$refs.npcTask.getUEContentTxt();
        this.npcTaskForm.notice = content;
        this.npcTaskForm.id = null;
        this.checkedData.forEach(element1 =>{
          this.dataMatVendor.forEach(element2 =>{
            if(element1.vendorCode === element2.vendorCode){
              element2.applyItemId = element1.applyItemId;
              element2.applyNo = element1.applyNo;
            }
          })
        })
        let data={
          ...this.npcTaskForm,
          npcTaskItemList:this.dataMaterialList,
          npcTaskVendorList:this.dataVendorList,
          npcTaskMappingList:this.dataMatVendor,
        }
        this.axios.post(this.lib.config.host.vendorDlv + "/vendormgt/npc/npcTask/saveAndPublish",data).then((res) => {
          if(res.data.success){
            // 发布后跳转到主页面
            this.$router.push('/Index/VendorDlv/NpcTask');
          }
        })
        .catch(function (error) {
            console.log(error);
        });
      }
    }
  };
</script>
